import CryptoJS from 'crypto-js';

// 加密方法
function encryptData(data, secretKey) {
  const encryptedData = CryptoJS.AES.encrypt(JSON.stringify(data), secretKey).toString();
  return encryptedData;
}

// 解密方法
function decryptData(encryptedData, secretKey) {
  const bytes = CryptoJS.AES.decrypt(encryptedData, secretKey);
  const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
  return decryptedData;
}

const secretKey = 'miaoka19512020520';

export const useLocalStorageSet = (key, value) => {
    const encrypted = encryptData(value, secretKey)
    localStorage.setItem(key, JSON.stringify(encrypted))
}

export const useLocalStorageGet = (key) => {
    const encrypted = localStorage.getItem(key)
    const _encrypted = decryptData(JSON.parse(encrypted), secretKey)
    return _encrypted
}
